package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.NewModel;
import com.zeng.jiudian.bean.NewModelExample;
import com.zeng.jiudian.bean.NewModelExample.Criteria;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.dao.NewDao;
import com.zeng.jiudian.mapper.NewMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/27 - 15:03
 */
public class NewDaoImpl implements NewDao
{
    private SqlSession sqlSession;

    @Override
    public int add(NewModel newModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newModel.setCreateTime(dateFormat.format(new Date()));
        newModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            count = mapper.insert(newModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NewModel> findAll(NewModel newModel)
    {
        List<NewModel> list = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            NewModelExample example = new NewModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(newModel.getTitle()))
            {
                criteria.andTitleLike("%" + newModel.getTitle() + "%");
            }
            if (!"".equals(newModel.getIsFrom()))
            {
                criteria.andIsFromLike("%" + newModel.getIsFrom() + "%");
            }
            if (!"".equals(newModel.getAuthor()))
            {
                criteria.andAuthorLike("%" + newModel.getAuthor() + "%");
            }
            if (!"无".equals(newModel.getType()))
            {
                criteria.andTypeLike("%" + newModel.getType() + "%");
            }
            if (!"".equals(newModel.getContent()))
            {
                criteria.andContentLike("%" + newModel.getContent() + "%");
            }
            if (!"".equals(newModel.getStartCreateTime()) && !"".equals(newModel.getEndCreateTime()))
            {
                criteria.andCreateTimeBetween(dateFormat.parse(newModel.getStartCreateTime()), dateFormat.parse(newModel.getEndCreateTime()));
            }
            else
            {
                if (!"".equals(newModel.getStartCreateTime()))
                {
                    criteria.andCreateTimeGreaterThanOrEqualTo(dateFormat.parse(newModel.getStartCreateTime()));
                }
                if (!"".equals(newModel.getEndCreateTime()))
                {
                    criteria.andCreateTimeLessThanOrEqualTo(dateFormat.parse(newModel.getEndCreateTime()));
                }
            }
            if (newModel.getIsShow() != null && newModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(newModel.getIsShow());
            }
            if (newModel.getRecommend() != null && newModel.getRecommend() >= 0)
            {
                criteria.andRecommendEqualTo(newModel.getRecommend());
            }
            if (newModel.getNewTypeId() != null && newModel.getNewTypeId() >= 0)
            {
                criteria.andNewTypeIdEqualTo(newModel.getNewTypeId());
            }

            if (!"无".equals(newModel.getOrder()))
            {
                if ("创建时间".equals(newModel.getOrder()))
                {
                    if ("降序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(newModel.getOrder()))
                {
                    if ("降序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
                else if ("浏览次数".equals(newModel.getOrder()))
                {
                    if ("降序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("view_num desc");
                    }
                    else if ("升序".equals(newModel.getLift()))
                    {
                        example.setOrderByClause("view_num asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(newModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(newModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = newModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException | ParseException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(NewModel newModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            NewModelExample example = new NewModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(newModel.getTitle()))
            {
                criteria.andTitleLike("%" + newModel.getTitle() + "%");
            }
            if (!"".equals(newModel.getIsFrom()))
            {
                criteria.andIsFromLike("%" + newModel.getIsFrom() + "%");
            }
            if (!"".equals(newModel.getAuthor()))
            {
                criteria.andAuthorLike("%" + newModel.getAuthor() + "%");
            }
            if (!"无".equals(newModel.getType()))
            {
                criteria.andTypeLike("%" + newModel.getType() + "%");
            }
            if (!"".equals(newModel.getContent()))
            {
                criteria.andContentLike("%" + newModel.getContent() + "%");
            }
            if (!"".equals(newModel.getStartCreateTime()) && !"".equals(newModel.getEndCreateTime()))
            {
                criteria.andCreateTimeBetween(dateFormat.parse(newModel.getStartCreateTime()), dateFormat.parse(newModel.getEndCreateTime()));
            }
            else
            {
                if (!"".equals(newModel.getStartCreateTime()))
                {
                    criteria.andCreateTimeGreaterThanOrEqualTo(dateFormat.parse(newModel.getStartCreateTime()));
                }
                if (!"".equals(newModel.getEndCreateTime()))
                {
                    criteria.andCreateTimeLessThanOrEqualTo(dateFormat.parse(newModel.getEndCreateTime()));
                }
            }
            if (newModel.getIsShow() != null && newModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(newModel.getIsShow());
            }
            if (newModel.getRecommend() != null && newModel.getRecommend() >= 0)
            {
                criteria.andRecommendEqualTo(newModel.getRecommend());
            }
            if (newModel.getNewTypeId() != null && newModel.getNewTypeId() >= 0)
            {
                criteria.andNewTypeIdEqualTo(newModel.getNewTypeId());
            }

            count = (int) mapper.countByExample(example);
        }
        catch (ParseException | IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public NewModel findById(int id)
    {
        NewModel model = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            model = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int update(NewModel newModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            count = mapper.updateByPrimaryKeySelective(newModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NewModel> findNewByRecommend()
    {
        List<NewModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            NewModelExample example = new NewModelExample();
            Criteria criteria = example.createCriteria();
            criteria.andTypeEqualTo("酒店新闻");
            criteria.andIsShowEqualTo(1);
            example.setOrderByClause("recommend desc");
            example.setStartRow(0);
            example.setPageSize(10);
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public List<NewModel> findFacByRecommend()
    {
        List<NewModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NewMapper mapper = sqlSession.getMapper(NewMapper.class);
            NewModelExample example = new NewModelExample();
            Criteria criteria = example.createCriteria();
            criteria.andTypeEqualTo("设施介绍");
            criteria.andIsShowEqualTo(1);
            example.setOrderByClause("recommend desc");
            example.setStartRow(0);
            example.setPageSize(10);
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }
}
